home *** CD-ROM | disk | FTP | other *** search
/ Pascal Super Library / Pascal Super Library (CW International)(1997).bin / BORL_TIP / TI1000 / TI1721.ASC < prev    next >
Text File  |  1993-10-21  |  8KB  |  331 lines

  1.  
  2.  
  3.  
  4.  
  5.  
  6.  
  7.  
  8.   PRODUCT  :  Borland Pascal                        NUMBER  :  1721
  9.   VERSION  :  All
  10.        OS  :  DOS
  11.      DATE  :  October 21, 1993                         PAGE  :  1/5
  12.  
  13.     TITLE  :  Does a Turbo Vision control have the input focus?
  14.  
  15.  
  16.  
  17.  
  18.   The following program demonstrates how to detect if a control
  19.   in a Turbo Vision dialog has received the focus. The key is the
  20.   cmReceivedFocus message, which gets sent to the owner of a
  21.   control whenever that control is about to receive the focus.
  22.   Here is sample code showing how to respond to the message:
  23.  
  24.   procedure TTrainDialog.HandleEvent(var Event: TEvent);
  25.   begin
  26.     if (Event.What = EvBroadCast)
  27.     and (Event.InfoPtr = MyInputLine)
  28.     and (Event.Command = cmReceivedFocus) then begin
  29.       MessageBox('Got cmReceivedFocus Message, nil, mfOkCancel);
  30.     end;
  31.     TDialog.HandleEvent(Event);
  32.   end;
  33.  
  34.   The above code sample assumes that an inputline has been inserted
  35.   into a dialog. Whenever the user TABs to the input line, then
  36.   the cmReceivedFocus message is sent to the control's owner. Note
  37.   that its not sent to the control itself, but to the control's
  38.   owner! Note also that this is a broadcast message. Of course
  39.   its not enough just to pick up on broadcast cmReceivedFocus
  40.   messages. You also have to detect which control has just
  41.   received the focus. To do this, you can use the InfoPtr field
  42.   of the incoming TEVENT record. This field will contain a
  43.   pointer to the control that is about to receive the focus. The
  44.   example code here literally compares the pointer to a second
  45.   copy of a pointer to that control. If they are equal, then
  46.   you know that you have the right control. Of course, the above
  47.   code assumes that you have explicitly saved and declared a
  48.   pointer to the inputline, as shown in the included example
  49.   program.
  50.       Now that you understand how cmReceivedFocus messages
  51.   work, you might be interested in seeing another way of handling
  52.   the same problem. This is to override the SetState function which
  53.   actually generates cmReceivedFocus messages. In the program
  54.   shown below, the following code fragment will enable and
  55.   disable the state of a command on the statusline, depending
  56.   on whether or not the control has the focus.
  57.  
  58.   procedure TMyButton.SetState(AState: Word; Enable: Boolean);
  59.   begin
  60.     inherited SetState(AState, Enable);
  61.  
  62.  
  63.  
  64.  
  65.  
  66.  
  67.  
  68.  
  69.  
  70.  
  71.  
  72.  
  73.  
  74.   PRODUCT  :  Borland Pascal                        NUMBER  :  1721
  75.   VERSION  :  All
  76.        OS  :  DOS
  77.      DATE  :  October 21, 1993                         PAGE  :  2/5
  78.  
  79.     TITLE  :  Does a Turbo Vision control have the input focus?
  80.  
  81.  
  82.  
  83.  
  84.     if (AState = sfFocused) and (Enable = False) then
  85.       DisableCommands([cmFoo]);
  86.     if (AState = sfFocused) and (Enable = True) then
  87.       EnableCommands([cmFoo]);
  88.   end;
  89.  
  90.   The key to the above code is that it checks for sfFocused
  91.   messages to find out whether the control is receiving or
  92.   losing the focus.
  93.          When running the following program, watch the status
  94.   line to see which control has the focus.
  95.   }
  96.  
  97.   program DlgFocus;
  98.   uses
  99.     App, Dialogs, Drivers, Menus, MsgBox, Validate, Views, Objects;
  100.  
  101.   const
  102.     cmDialogBox = 101;
  103.     cmTest      = 102;
  104.     cmButton    = 103;
  105.     cmInput      = 104;
  106.     InputLength = 128;
  107.     TheMessage = 'The Input line just received focus.' + #13#10 +
  108.                  'Chose OK to set Ok in the Inputline' + #13#10 +
  109.                  'Chose Cancel to set Cancel in the InputLine';
  110.  
  111.   type
  112.     PMyButton = ^TMyButton;
  113.     TMyButton = Object(TButton)
  114.       procedure SetState(AState: Word; Enable: Boolean); virtual;
  115.     end;
  116.  
  117.     PTrainDialog = ^TTrainDialog;
  118.     TTrainDialog = Object(TDialog)
  119.         MyInputLine: PInputLine;
  120.       constructor Init(Bounds: TRect; ATitle: String);
  121.       procedure HandleEvent(var Event: TEvent); virtual;
  122.     end;
  123.  
  124.     TMyApp = Object(TApplication)
  125.       constructor Init;
  126.       procedure InitStatusLine; virtual;
  127.  
  128.  
  129.  
  130.  
  131.  
  132.  
  133.  
  134.  
  135.  
  136.  
  137.  
  138.  
  139.  
  140.   PRODUCT  :  Borland Pascal                        NUMBER  :  1721
  141.   VERSION  :  All
  142.        OS  :  DOS
  143.      DATE  :  October 21, 1993                         PAGE  :  3/5
  144.  
  145.     TITLE  :  Does a Turbo Vision control have the input focus?
  146.  
  147.  
  148.  
  149.  
  150.       procedure HandleEvent(var Event: TEvent); virtual;
  151.       procedure DialogBox;
  152.     end;
  153.  
  154.   procedure TMyButton.SetState(AState: Word; Enable: Boolean);
  155.   begin
  156.     inherited SetState(AState, Enable);
  157.     if (AState = sfFocused) and (Enable = False) then
  158.       DisableCommands([cmButton]);
  159.     if (AState = sfFocused) and (Enable = True) then
  160.       EnableCommands([cmButton]);
  161.   end;
  162.  
  163.   constructor TTrainDialog.Init(Bounds: TRect; ATitle: String);
  164.   var
  165.     R: TRect;
  166.     Control: PView;
  167.     S: String;
  168.   begin
  169.     TDialog.Init(Bounds, ATitle);
  170.     R.Assign(2, 2, 37, 4);
  171.     Insert(New(PMyButton, Init(R, 'Test', cmTest, bfNormal)));
  172.     R.Assign(3, 5, 37, 6);
  173.     MyInputLine := New(PInputLine, Init(R, InputLength));
  174.     Insert(MyInputLine);
  175.     R.Assign(2, 4, 24, 5);
  176.     Insert(New(PLabel, Init(R, 'Delivery instructions',
  177.        MyInputLine)));
  178.     R.Assign(2, 7, 37, 9);
  179.     Insert(New(PButton, Init(R, 'O~k~', cmOk, bfDefault)));
  180.  
  181.     S := '32';
  182.     SetData(S);
  183.   end;
  184.  
  185.   procedure TTrainDialog.HandleEvent(var Event: TEvent);
  186.   var
  187.     Result: Word;
  188.     S: String;
  189.   begin
  190.     if (Event.What = EvBroadCast) and
  191.        (Event.InfoPtr = MyInputLine) then
  192.       case Event.Command of
  193.  
  194.  
  195.  
  196.  
  197.  
  198.  
  199.  
  200.  
  201.  
  202.  
  203.  
  204.  
  205.  
  206.   PRODUCT  :  Borland Pascal                        NUMBER  :  1721
  207.   VERSION  :  All
  208.        OS  :  DOS
  209.      DATE  :  October 21, 1993                         PAGE  :  4/5
  210.  
  211.     TITLE  :  Does a Turbo Vision control have the input focus?
  212.  
  213.  
  214.  
  215.  
  216.         cmReceivedFocus: EnableCommands([cmInput]);
  217.         cmReleasedFocus: DisableCommands([cmInput]);
  218.       end;
  219.     TDialog.HandleEvent(Event);
  220.   end;
  221.  
  222.   constructor TMyApp.Init;
  223.   begin
  224.     inherited Init;
  225.     DisableCommands([cmInput,cmButton]);
  226.   end;
  227.  
  228.   procedure TMyApp.HandleEvent(var Event: TEvent);
  229.   begin
  230.     TApplication.HandleEvent(Event);
  231.     if Event.What = EvCommand then begin
  232.       case Event.Command of
  233.         cmDialogBox: DialogBox;
  234.       else
  235.         Exit;
  236.       end;
  237.       ClearEvent(Event);
  238.     end;
  239.   end;
  240.  
  241.   procedure TMyApp.InitStatusLine;
  242.   var R: TRect;
  243.   begin
  244.     GetExtent(R);
  245.     R.A.Y := R.B.Y - 1;
  246.     StatusLine := New(PStatusLine, Init(R,
  247.       NewStatusDef(0, $FFFF,
  248.         NewStatusKey('', kbF10, cmMenu,
  249.         NewStatusKey('~Alt-X~ Exit', kbAltX, cmQuit,
  250.         NewStatusKey('~Alt-D~ Dialog', kbAltD, cmDialogBox,
  251.         NewStatusKey('~Alt-F4~ Button Focused?',
  252.                  kbAltF4, cmButton,
  253.         NewStatusKey('~Alt-F5~ InputLine Focused?',
  254.                  kbAltF5, cmInput,
  255.         nil))))),
  256.       nil)
  257.     ));
  258.   end;
  259.  
  260.  
  261.  
  262.  
  263.  
  264.  
  265.  
  266.  
  267.  
  268.  
  269.  
  270.  
  271.  
  272.   PRODUCT  :  Borland Pascal                        NUMBER  :  1721
  273.   VERSION  :  All
  274.        OS  :  DOS
  275.      DATE  :  October 21, 1993                         PAGE  :  5/5
  276.  
  277.     TITLE  :  Does a Turbo Vision control have the input focus?
  278.  
  279.  
  280.  
  281.  
  282.   procedure TMyApp.DialogBox;
  283.   var
  284.     R: TRect;
  285.     D: PDialog;
  286.   begin
  287.     R.Assign(20,5,60,15);
  288.     D := New(PTrainDialog, Init(R, 'Hit Tab, watch statusline'));
  289.     if ValidView(D) <> Nil then DeskTop^.ExecView(D);
  290.     Dispose(D, Done);
  291.   end;
  292.  
  293.   var
  294.     A: TMyApp;
  295.   begin
  296.     A.Init;
  297.     A.Run;
  298.     A.Done;
  299.   end.
  300.  
  301.  
  302.   DISCLAIMER: You have the right to use this technical information
  303.   subject to the terms of the No-Nonsense License Statement that
  304.   you received with the Borland product to which this information
  305.   pertains.
  306.  
  307.  
  308.  
  309.  
  310.  
  311.  
  312.  
  313.  
  314.  
  315.  
  316.  
  317.  
  318.  
  319.  
  320.  
  321.  
  322.  
  323.  
  324.  
  325.  
  326.  
  327.  
  328.  
  329.  
  330.  
  331.